chrome.storage.local.get("adFilterConfig",function(result){
    if(result.adFilterConfig){
        preConfig=result.adFilterConfig;
    }else{
        preConfig=jsDb.get('adFilterConfig');
        if(!preConfig){
            preConfig='{"filters":[{"match":"*","schedule":1000,"script":"console.log(\'adfilter running\');"}]}';
        }
    }
    document.getElementById('scrpt').value=preConfig;
});
chrome.storage.local.get("configws",function(result){
        if(result){
            document.getElementById('configws').value=result.configws;
        }
});

function save(){
	var text=document.getElementById('scrpt').value;
	if(text.trim()!=null&&text.trim()!=''){
		try{
			var config=JSON.parse(text);
			check(config.filters);
			jsDb.set('adFilterConfig',text);
			chrome.storage.local.set({'adFilterConfig': text});
			chrome.storage.local.set({'change': "true"});
		}catch(error){
			alert('json格式错误');
		}
	}
};
function check(filters){
    if(filters){
        for(let i=0;i<filters.length;i++){
            let filter=filters[i];
            if(!filter.script){
                console.log('执行脚本未配置');
            }
            if(!filter.match){
                console.log('匹配规则未配置');
            }
        }
    }
}
function reset(){
	jsDb.set('adFilterConfig','');
	chrome.storage.local.set({'adFilterConfig': ''});
	document.getElementById('scrpt').value='';
}
function add(){
    var text=document.getElementById('scrpt').value;
	if(text.trim()==null||text.trim()==''){
		text="{'filters':[]}";
	}
	try{
        var config=JSON.parse(text);
        var filter={};
        filter.match=document.getElementById("host").value;
        filter.schedule=document.getElementById("schedule").value;
        filter.script=document.getElementById("script").value;
        filter.regx=document.getElementById("regx").value;
        config.filters.push(filter);
        check(config.filters);
        document.getElementById('scrpt').value=JSON.stringify(config);
        save();
    }catch(error){
        alert('json格式错误');
    }
}
function remove(){
    var text=document.getElementById('scrpt').value;
	if(text.trim()==null||text.trim()==''){
		text="{'filters':[]}";
	}
	try{
        var config=JSON.parse(text);
        var filter={};
        filter.match=document.getElementById("host").value;
        for(let i=0;i<config.filters.length;i++){
            if(config.filters[i].match==filter.match){
                delete config.filters[i];
            }
        }
        check(config.filters);
        document.getElementById('scrpt').value=JSON.stringify(config);
        save();
    }catch(error){
        alert('json格式错误');
    }
}
function configWs(){
    var text=document.getElementById('configws').value;
    jsDb.set('configws',text);
    chrome.storage.local.set({'configws': text});
    ws.connect(result.configws).open().message(function(data){
                console.log(data);
    });
}
document.getElementById('save').onclick=save;
document.getElementById('reset').onclick=reset;
document.getElementById('add').onclick=add;
document.getElementById('conf').onclick=configWs;

